package com.example.myapplication.livedata.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.bean.ProductInfo;
import com.example.myapplication.databinding.FragmentLiveDataMutableLiveDataBinding;
import com.example.myapplication.databinding.FragmentLiveDataViewModelBinding;
import com.example.myapplication.livedata.model.LiveDataViewModel;
import com.example.myapplication.livedata.model.SavedStateViewModel;

/**
 * create an instance of this fragment.
 */
public class LiveDataViewModelFragment extends Fragment {

    FragmentLiveDataViewModelBinding binding;
    LiveDataViewModel viewModel;
    SavedStateViewModel stateViewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = FragmentLiveDataViewModelBinding.inflate(getLayoutInflater());
        binding.setLifecycleOwner(this);

        viewModel = new ViewModelProvider(this).get(LiveDataViewModel.class);
        stateViewModel = new ViewModelProvider(this).get(SavedStateViewModel.class);

        binding.setViewModel(viewModel);
        binding.setStateModel(stateViewModel);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {

        binding.btnSetProduct1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductInfo productInfo = new ProductInfo();
                productInfo.setName("stateViewModel商品1");
                productInfo.setPrice(100);
                productInfo.setDes("这个ViewModel有恢复数据的特性，使用的是set方法进行保存的");
                stateViewModel.setProduct1(productInfo);
            }
        });

        binding.btnSetProduct2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductInfo productInfo = new ProductInfo();
                productInfo.setName("stateViewModel商品2");
                productInfo.setPrice(100);
                productInfo.setDes("这个ViewModel有恢复数据的特性，使用的是Provider方法进行保存的");
                stateViewModel.setProduct2(productInfo);
                binding.setStateModel(stateViewModel);
            }
        });

        binding.btnUpdateProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductInfo productInfo = new ProductInfo();
                productInfo.setName("viewModel商品");
                productInfo.setPrice(100);
                productInfo.setDes("这个ViewModel没有恢复数据的特性");
                viewModel.setProduct(productInfo);
            }
        });
    }

    @NonNull
    @Override
    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        return super.getDefaultViewModelProviderFactory();
    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return super.getViewModelStore();
    }
}
